﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OpGen.Compiler.Parser;

namespace OpGen.Compiler.Analyser.UI.Dialogs
{
    public partial class frmMain : Form
    {
        private ProjectElement _Project;

        public frmMain()
        {
            InitializeComponent();
        }

        private void öffnenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var FD = new OpenFileDialog())
            {
                FD.Title = "Quelldatei öffnen";
                FD.Filter = "Alle Dateien|*.*";
                if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var P = new OpGenParser(new System.IO.FileInfo(FD.FileName));

                    P.Execute();
                    FillErrorList(P);
                    BuildTree(P.Project);

                    _Project = P.Project;
                }
            }
        }

        private void FillErrorList(OpGenParser Parser)
        {
            listView1.Items.Clear();

            foreach (var Error in Parser.ErrorList.OrderBy(m => m.MessageType).ToList())
            {
                listView1.Items.Add(new ErrorListViewItem(Error));
            }
        }

        private void BuildTree(ProjectElement Project)
        {
            var S = new Stack<ElementTreeViewItem>();
            var BaseElement = new ElementTreeViewItem(Project);
            var IncludeDirectory = new TreeNode() { Text = "Include", ImageIndex = 20, SelectedImageIndex = 20 };

            foreach (var item in Project.IncludeFiles)
            {
                IncludeDirectory.Nodes.Add(new TreeNode() { Text = item.Name, ImageIndex = 2, SelectedImageIndex = 2 });
            }

            BaseElement.Nodes.Add(IncludeDirectory);

            S.Push(BaseElement);

            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(BaseElement); 

            while (S.Count > 0)
            {
                var CurrentNode = S.Pop();

                foreach (var item in CurrentNode.Element.ChildElements)
                {
                    var NewNode = new ElementTreeViewItem(item);

                    CurrentNode.Nodes.Add(NewNode);
                    S.Push(NewNode);
                }
            }

            BaseElement.Expand();
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void codeErzeugenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var F = new SaveFileDialog())
            {
                F.Title = "Zieldatei";
                F.Filter = "CS Quellcode Datei (*.cs)|*.cs|VB Quellcode Datei (*.vb)|*.vb";

                if (F.ShowDialog() == DialogResult.OK)
                {
                    var FileInfo = new System.IO.FileInfo(F.FileName);
                    OpGen.Compiler.Generator.CodeWriter CodeWriter;

                    switch (F.FilterIndex)
                    {
                        case 1:
                            CodeWriter = new OpGen.Compiler.Generator.CS.CSCodeWriter(FileInfo.Create());
                            break;
                        case 2:
                            CodeWriter = new OpGen.Compiler.Generator.VB.VBCodeWriter(FileInfo.Create());
                            break;
                        default:
                            CodeWriter = new OpGen.Compiler.Generator.CS.CSCodeWriter(FileInfo.Create());
                            break;
                    }                  
                    
                    OpGen.Compiler.Generator.Generator.GenerateCode(_Project, CodeWriter);
                }

            }
        }
        

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            richTextBox1.Text = "";

            if (treeView1.SelectedNode is ElementTreeViewItem)
            {
                if (treeView1.SelectedNode != null)
                {
                    var Element = ((ElementTreeViewItem)treeView1.SelectedNode).Element;

                    richTextBox1.Text = Element.ToString();
                }
            }
        }
    }
}
